// 全局app.js文件，用于处理tabbar相关逻辑

// 获取当前页面路径
export const getCurrentPagePath = () => {
  try {
    // 尝试获取当前页面堆栈
    const pages = getCurrentPages();
    if (pages && pages.length > 0) {
      const currentPage = pages[pages.length - 1];
      // 确保route属性存在
      if (currentPage.route) {
        return '/' + currentPage.route;
      } else if (currentPage.__route__) {
        // 兼容某些情况下可能使用__route__
        return '/' + currentPage.__route__;
      } else if (currentPage.is) {
        // 兼容另一种可能的路径表示
        return '/' + currentPage.is;
      }
    }
    
    // 如果在小程序环境下，尝试从其他方式获取
    if (typeof wx !== 'undefined') {
      // 可能的情况: wx.__route__
      if (wx && wx.__route__) {
        return '/' + wx.__route__;
      }
      
      // 如果有全局应用实例
      const app = getApp();
      if (app && app.globalData && app.globalData.currentPath) {
        return app.globalData.currentPath;
      }
    }
    
    // 所有方法都失败时返回默认值
    console.log('无法获取当前页面路径，返回默认首页路径');
    return '/pages/index/index';
  } catch (e) {
    console.error('获取当前页面路径出错:', e);
    return '/pages/index/index';
  }
};

// 判断是否是tabbar页面
export const isTabbarPage = (path) => {
  const tabbarPages = [
    '/pages/index/index',
    '/pages/record/list',
    '/pages/diet/index',
    '/pages/community/index',
    '/pages/profile/index'
  ];
  
  return tabbarPages.includes(path);
};

// 获取tabbar配置
export const getTabbarConfig = () => {
  return [
    {
      pagePath: '/pages/index/index',
      text: '首页',
      iconPath: '/static/images/tabbar/home.png',
      selectedIconPath: '/static/images/tabbar/home-active.png'
    },
    {
      pagePath: '/pages/record/list',
      text: '血糖',
      iconPath: '/static/images/tabbar/blood.png',
      selectedIconPath: '/static/images/tabbar/blood-active.png'
    },
    {
      pagePath: '/pages/diet/index',
      text: '饮食',
      iconPath: '/static/images/tabbar/diet.png',
      selectedIconPath: '/static/images/tabbar/diet-active.png'
    },
    {
      pagePath: '/pages/community/index',
      text: '社区',
      iconPath: '/static/images/tabbar/community.png',
      selectedIconPath: '/static/images/tabbar/community-active.png'
    },
    {
      pagePath: '/pages/profile/index',
      text: '我的',
      iconPath: '/static/images/tabbar/profile.png',
      selectedIconPath: '/static/images/tabbar/profile-active.png'
    }
  ];
}; 